﻿using FYSJcrm.Core.Common;
using FYSJcrm.Core.IRepository.SystemManage;
using FYSJcrm.Core.Model.SystemManage;
using FYSJcrm.Core.Web.Areas.SystemManage.Models.NavManageModels;
using FYSJcrm.Core.Web.Extensions.FilterExtensions;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FYSJcrm.Core.Web.Areas.SystemManage.Controllers
{
    [Area(nameof(SystemManage))]
    public class NavManageController : Controller
    {
        private readonly INavigationRepository _navigationRepository;

        public NavManageController(INavigationRepository navigationRepository)
        {
            _navigationRepository = navigationRepository;
        }

        [CheckPermission("NavManage.Show")]
        public async Task<IActionResult> List()
        {
            NavManageViewModel model = new NavManageViewModel
            {
                Navigations = await _navigationRepository.Select.OrderBy(p => p.SortId).ToTreeListAsync()
            };

            return View(model);
        }

        [CheckPermission("NavManage.Add")]
        public async Task<IActionResult> Add()
        {
            NavManageAddViewModel model = new NavManageAddViewModel
            {
                ParentNav = await _navigationRepository.Select.Where(p => p.ParentId == 0).OrderBy(p => p.SortId).ToListAsync()
            };

            return View(model);
        }

        [HttpPost]
        [CheckPermission("NavManage.Add")]
        public async Task<IActionResult> Add(NavManageAddModel model)
        {
            StringBuilder strAction = new StringBuilder();

            foreach (var str in model.ActionTypes)
            {
                strAction.Append(str + ",");
            }

            NavigationEntity navigation = new NavigationEntity
            {
                ParentId = model.ParentId,
                Name = model.Name,
                Title = model.Title,
                IconUrl = model.ParentId == 0 ? "&" + model.IconUrl : model.IconUrl,
                LinkUrl = model.LinkUrl,
                NavType = "System",
                ActionType = strAction.ToString(),
                IsLock = model.IsLock,
                SortId = model.SortId
            };

            navigation = await _navigationRepository.InsertAsync(navigation);
            return navigation.Id > 0 ? Json(new AjaxResult { Code = HttpCode.OK, Data = "添加栏目成功！" })
                : Json(new AjaxResult { Code = HttpCode.BadRequest, ErrorMsg = "添加栏目失败！" });
        }

        [CheckPermission("NavManage.Edit")]
        public async Task<IActionResult> Edit(int id)
        {
            NavManageEditViewModel model = new NavManageEditViewModel
            {
                ParentNav = await _navigationRepository.Select.Where(p => p.ParentId == 0).OrderBy(p => p.SortId).ToListAsync(),
                Navigation = await _navigationRepository.Select.Where(p => p.Id == id).FirstAsync()
            };

            return View(model);
        }

        [HttpPost]
        [CheckPermission("NavManage.Edit")]
        public async Task<IActionResult> Edit(NavManageEditModel model)
        {
            var navigation = await _navigationRepository.Select.Where(p => p.Id == model.Id).FirstAsync();
            if (navigation == null)
            {
                return Json(new AjaxResult { Code = HttpCode.BadRequest, ErrorMsg = "你要修改的栏目不存在！" });
            }

            StringBuilder strAction = new StringBuilder();

            foreach (var str in model.ActionTypes)
            {
                strAction.Append(str + ",");
            }

            navigation.Name = model.Name;
            navigation.ParentId = model.ParentId;
            navigation.Title = model.Title;
            navigation.IconUrl = "&" + model.IconUrl;
            navigation.LinkUrl = model.LinkUrl;
            navigation.SortId = model.SortId;
            navigation.IsLock = model.IsLock;
            navigation.ActionType = strAction.ToString();

            await _navigationRepository.UpdateAsync(navigation);

            return Json(new AjaxResult { Code = HttpCode.OK, Data = "修改栏目成功！" });
        }

        [HttpPost]
        [CheckPermission("NavManage.Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            var flag = await _navigationRepository.DeleteAsync(p => p.Id == id);

            return flag > 0 ? Json(new AjaxResult { Code = HttpCode.OK, Data = "删除栏目成功！" })
               : Json(new AjaxResult { Code = HttpCode.BadRequest, ErrorMsg = "删除栏目失败！" });
        }

        [HttpPost]
        [CheckPermission("NavManage.Delete")]
        public async Task<IActionResult> BatchDelete(int[] selectedIds)
        {
            if (selectedIds.Length == 0)
            {
                return Json(new AjaxResult { Code = HttpCode.BadRequest, ErrorMsg = "请选择要删除的数据" });
            }

            var flag = await _navigationRepository.DeleteAsync(p => selectedIds.Contains(p.Id));

            return flag > 0 ? Json(new AjaxResult { Code = HttpCode.OK, Data = "成功删除" + flag + "个栏目，删除失败" + (selectedIds.Length - flag) + "个栏目" })
                : Json(new AjaxResult { Code = HttpCode.BadRequest, ErrorMsg = "删除栏目失败！" });
        }
    }
}
